/* eslint-disable @typescript-eslint/no-explicit-any */
export type InjectPosition = 'before' | 'after'

export function inject<T extends (...args: any) => any>(
  position: InjectPosition,
  func: T,
  handler?: (...args: Parameters<T>) => void,
): T {
  return ((...args: Parameters<T>): ReturnType<T> => {
    if (position === 'before') handler?.(...args)
    const result = func(...(args as any))
    if (position === 'after') handler?.(...args)
    return result
  }) as T
}
